﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Xml;
using System.Reflection;
using BlueLotus7.ArchiveSystem.Interfaces;

namespace BlueLotus7.ArchiveSystem.Common
{
    [DataContract]
    public class MonitorTask : ReportableTask
    {
        [DataContract]
        public enum TaskExecutionType
        {
            [EnumMember]
            Once = 0,
            [EnumMember]
            Every10Sec = 10,
            [EnumMember]
            Every30Sec = 30,
            [EnumMember]
            Every1Min = 60,
            [EnumMember]
            Every2Min = 60 * 2,
            [EnumMember]
            Every5Min = 60 * 5,
            [EnumMember]
            Every10Min = 60 * 10,
            [EnumMember]
            Every30Min = 60 * 30,
            [EnumMember]
            Every1Hour = 60 * 60,
            [EnumMember]
            Every2Hour = 60 * 60 * 2,
            [EnumMember]
            Every4Hour = 60 * 60 * 4,
            [EnumMember]
            Every12Hour = 60 * 60 * 12,
            [EnumMember]
            EveryDay = 60 * 60 * 24,
            [EnumMember]
            Every7Days = 60 * 60 * 24 * 7
        }

        [DataMember]
        public TaskExecutionType ExecutionType
        {
            get;
            set;
        }

        [DataMember]
        public DateTime LastUpdatedStamp
        {
            get;
            set;
        }

        [DataMember]
        public bool IsNightOperationOnly
        {
            get;
            set;
        }

        public bool NeedExecute(DateTime nowTime)
        {
            bool needExecute = false;

            if ((int)this.Status < 10)
            {
                if (this.LastExecutedStamp == null)
                {
                    needExecute = true;
                }
                else if (this.ExecutionType != TaskExecutionType.Once)
                {
                    if (!this.IsNightOperationOnly || (nowTime.Hour > 1 && nowTime.Hour < 6))
                    {
                        needExecute = ((nowTime - LastExecutedStamp.Value).Minutes > (int)this.ExecutionType);
                    }
                }
            }

            return needExecute;
        }
    }

}
